/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainInterface.java
 *
 * Created on 23/06/2010, 22:50:48
 */
package gui;

import java.awt.Color;
import javax.swing.UIManager;

/**
 *
 * @author samir
 */
public class MainInterface extends javax.swing.JFrame {

    Integer adivinha_num = -1;
    Integer palpite_anterior = -1;
    Color original;

    /** Creates new form MainInterface */
    public MainInterface() {
        initComponents();
        initGame();
        original = input.getBackground();
    }

    public void initGame() {
        adivinha_num = geraNumero();
        System.out.println(adivinha_num);
        palpite_anterior = -1;
        label_resultado.setText("Por favor, digite o primeiro palpite:");
        input.setBackground(original);
        input.setText("");
        palpite_anterior_label.setText("");
        jButton1.setEnabled(true);
    }

    public Integer geraNumero() {
        Integer n = 1 + (int) (Math.random() * 1000);
        return n;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        input = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        label_resultado = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        palpite_anterior_label = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Adivinha!");
        setMinimumSize(new java.awt.Dimension(502, 105));

        input.setBackground(javax.swing.UIManager.getDefaults().getColor("nb.errorForeground"));
        input.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inputActionPerformed(evt);
            }
        });

        jLabel1.setText("Eu tenho um número de 1 a 1000. Você consegue adivinhar meu número?");

        label_resultado.setText("Por favor, digite o primeiro palpite:");

        jButton2.setLabel("Novo jogo");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setLabel("Adivinhar!");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        palpite_anterior_label.setFont(new java.awt.Font("DejaVu Sans", 0, 24)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 502, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(input, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(palpite_anterior_label)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 191, Short.MAX_VALUE)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(label_resultado, javax.swing.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 105, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(label_resultado, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(palpite_anterior_label)
                            .addComponent(input, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(14, Short.MAX_VALUE))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        System.out.println("Novo o jogo.");
        initGame();
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (input.getText().isEmpty()) {
            label_resultado.setText("Você não digitou nada. ¬¬");
            return;
        }
        try {
            Integer palpite_atual = Integer.parseInt(input.getText());


            if (palpite_atual < 1 || palpite_atual > 1000) {
                label_resultado.setText("Este número está fora do intervalo estabelecido.");
                return;
            }
            if (palpite_atual.equals(adivinha_num)) {
                label_resultado.setText("Você acertou!!!");
                input.setBackground(Color.green);
                jButton1.setEnabled(false);
                return;
            }
            palpite_anterior_label.setText(palpite_atual.toString());
            input.setText("");
            // Mostra resultado na label
            if (palpite_atual < adivinha_num) {
                label_resultado.setText("Muito baixo");
            } else if (palpite_atual > adivinha_num) {
                label_resultado.setText("Muito alto");
            }
            // Muda cor
            System.out.println("PB: " + palpite_anterior.intValue());
            if (palpite_anterior.intValue() != -1) {
                Integer dist_anterior = Math.abs(adivinha_num - palpite_anterior);
                Integer dist_atual = Math.abs(adivinha_num - palpite_atual);

                System.out.println("DB: " + dist_anterior.toString());
                System.out.println("DA: " + dist_atual.toString());

                if (dist_atual < dist_anterior) {
                    input.setBackground(Color.red);
                } else {
                    input.setBackground(Color.blue);
                }
            }
            palpite_anterior = palpite_atual.intValue();
        } catch (Exception e) {
            label_resultado.setText("Você não digitou um número inteiro.");
            input.setText("");
            input.setBackground(original);
        }
}//GEN-LAST:event_jButton1ActionPerformed

    private void inputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputActionPerformed
        jButton1ActionPerformed(evt);
    }//GEN-LAST:event_inputActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                }
                new MainInterface().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField input;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel label_resultado;
    private javax.swing.JLabel palpite_anterior_label;
    // End of variables declaration//GEN-END:variables
}
